package cn.fengwoo.springBoot.handle;

import cn.fengwoo.springBoot.entity.Result;
import cn.fengwoo.springBoot.enums.ResultEnum;
import cn.fengwoo.springBoot.exception.FengwooException;
import cn.fengwoo.springBoot.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by fangdaji on 2018/1/11.
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof FengwooException) {
            FengwooException fengwooException = (FengwooException) e;
            return ResultUtil.error(fengwooException.getResultEnum());
        }else {
            logger.error("【系统异常】{}", e);
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }
}
